/**
* 
* Copyright [2007] [Sunny Liu]
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package ${appConfig.outputPackageName}.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import ${appConfig.outputPackageName}.web.view.FlexXMLView;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractSpringController extends AbstractController {

	public static final int ACTION_DEFAULT = 0;
	public static final int ACTION_FIND_ALL = 0;
	public static final int ACTION_FIND_BY_KEY = 1;
	public static final int ACTION_ADD_INIT = 10;
	public static final int ACTION_ADD = 11;
	public static final int ACTION_UPDATE_INIT = 20;
	public static final int ACTION_UPDATE = 21;
	public static final int ACTION_DELETE = 31;
	public static final String REQUEST_PARAM_ACTION = "action";
	
	
	private static Logger log = LoggerFactory.getLogger(AbstractSpringController.class);
	
	
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception 
	{
		ActionBean action = new ActionBean();
		BeanUtils.populate(action, request.getParameterMap());
		Object commandObject = null;
		ServerProcessStatus status = new ServerProcessStatus();
		if(action.getAction()!= ACTION_FIND_ALL)
		{
			commandObject = getCommandObject();
			BeanUtils.populate(commandObject, request.getParameterMap());
		}
		status.setAction(action.getAction());
		try{
			switch(action.getAction())
			{
				case ACTION_FIND_ALL:
					findAll(status, commandObject, request,	response);
					break;
				case ACTION_FIND_BY_KEY:
					findByKey(status, commandObject, request,	response);
					break;	
				case ACTION_ADD:
					add(status, commandObject, request,	response);
					break;
				case ACTION_UPDATE:
					update(status, commandObject, request,	response);
					break;
				case ACTION_DELETE:
					delete(status, commandObject, request,	response);
					break;
				default:
					handleOtherAction(status, commandObject, request,	response);
					break;		
			}
		}catch(Exception ex){
			log.error("Failed to process action " + action.getAction() , ex);
			status.setError(ex);
		}		
		String viewName = getViewName();
		if(action.isXmlView()){
			viewName = FlexXMLView.NAME;
		}		
		ModelAndView mv = new ModelAndView(viewName);		
		mv.addObject(ServerProcessStatus.SERVER_PROCESS_STATUS, status);
		return mv;
	}
	
	protected abstract Object getCommandObject();
	protected abstract void findAll(ServerProcessStatus status, Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response);
	protected abstract void findByKey(ServerProcessStatus status, Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response);
	protected abstract void add(ServerProcessStatus status, Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response);
	protected abstract void update(ServerProcessStatus status, Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response);
	protected abstract void delete(ServerProcessStatus status, Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response);
	protected abstract String getViewName();
	
	protected void handleOtherAction(ServerProcessStatus status, Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response)
	{
		throw new IllegalArgumentException("Inherited controller should implement this method");
	}

}
